<template>
  <div class="radius-container" v-show = "showFlag">
    <div class="border-b pad-10 font-w font-15 topbar-style">验收规则</div>
    <div class="pad-20">
      提示：此处编为微客申请验收的表单内容，至少一个字段，不修改则为默认值
      <el-table style="width: 100%" :data="dataList">
        <el-table-column min-width="250px" label="类型" prop="ruleType">
          <template slot-scope="scope" align="center">
            <template v-if="myDict.ruleType">
              {{ myDict.ruleType[scope.row.ruleType]}}
            </template>
          </template>
        </el-table-column>
        <el-table-column min-width="250px" label="字段名称" prop="ruleName">

          <template slot-scope="scope" align="center">
            <template v-if="scope.row.sortNo == 0">
              {{scope.row.ruleName}}
            </template>
            <template v-else>
              <el-input style="width: 150px" v-model="scope.row.ruleName"></el-input>
            </template>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" label="必填" prop="needFlag">
          <template slot-scope="scope" align="center">
            <template  v-if="scope.row.sortNo == 0">
              <el-checkbox v-model="needFlagYes" disabled></el-checkbox>
            </template>
            <template v-else>
              <el-checkbox v-model="scope.row.needFlag == '1'? true:false" @change="changeNeedFlag(scope.row)"></el-checkbox>
            </template>
          </template>
        </el-table-column>
        <el-table-column min-width="250px" label="设置" prop="ruleSettingType">
          <template slot-scope="scope" align="center">
            输入类型:
            <template v-if="scope.row.sortNo == 0">
              手机号
            </template>
            <template v-else>
              <template v-if="scope.row.ruleType == '1'">
                <el-select v-model="scope.row.ruleSettingType" placeholder="请选择">
                  <el-option
                    v-for="(value,key) in myDict.textDict"
                    :key="key"
                    :label="value"
                    :value="key">
                  </el-option>
                </el-select>
              </template>
              <template v-if="scope.row.ruleType == '2'">
                <el-select v-model="scope.row.ruleSettingType" placeholder="请选择">
                  <el-option
                    v-for="(value,key) in myDict.fileDict"
                    :key="key"
                    :label="value"
                    :value="key">
                  </el-option>
                </el-select>
              </template>
            </template>
          </template>
        </el-table-column>
        <el-table-column min-width="250px" label="操作">
          <template slot-scope="scope">
            <template v-if="scope.row.sortNo == 0">
              无
            </template>
            <template v-else>
                <a @click="upRecord(scope.$index)"><span style="color: #2d8cf0">上移</span></a>
                &nbsp;&nbsp;
                <a @click="downRecord(scope.$index)"><span style="color: #2d8cf0">下移</span></a>
                &nbsp;&nbsp;
                <a @click="deleteRecord(scope.$index)"><span style="color: red">删除</span></a>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <el-select v-model="ruleType" placeholder="请选择">
        <el-option
          v-for="(value,key) in myDict.ruleType"
          :key="key"
          :label="value"
          :value="key">
        </el-option>
      </el-select>
      <el-button type="primary" @click="addRule">新增一个字段</el-button>
      <el-button type="primary" plain  @click="showRuleType">预览</el-button>
    </div>


    <!-- 添加内部微客弹窗 -->
    <el-dialog title="" :visible.sync="visible" :close-on-press-escape="true" width="400px" >
      <div>
        <img src="https://enterpriseimg.joinsun.vip/upload/validate_sample.png" v-if="" style="width: 400px;height:600px">
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
  import elDragDialog from '@/directive/el-dragDialog' // base on element-ui

  export default {
    props: ['dict'],
    data() {
      return {
        needFlagYes: true,
        ruleType: '1',
        checked: '',
        dataList: [],
        acceptRuleList: [],
        myDict: {},
        visible: false,
        showFlag: false
      }
    },
    directives: {
      // 拖动窗口
      elDragDialog
    },
    // 初始化页面
    created() {
    },
    mounted() {
      // 初始化数据
      this.initAcceptRule()
      this.initDataInfo()
      this.initListener()
    },
    methods: {
      initAcceptRule() {
        const data = {
          ruleType: '1',
          ruleName: '手机号',
          needFlag: '1',
          ruleSettingType: '12',
          sortNo: 0
        }
        const dataList = this.dataList
        dataList.push(data)
        this.dataList = dataList
      },
      initDataInfo() {
        const that = this
        that.$bus.$on('project.task.edit.taskData', function(taskData) {
          const dataList = taskData.taskAcceptRuleDTOS
          dataList.forEach(e => {
            if (e.needFlag === 1) {
              e.needFlag = '1'
            } else {
              e.needFlag = '0'
            }
            e.ruleSettingType = e.ruleSettingType + ''
            if (e.ruleType === 1) {
              e.ruleType = e.ruleType + ''
            }
          })
          that.dataList = dataList
          that.$bus.$off('project.task.edit.taskData')
        })
      },
      initListener() {
        const that = this
        that.$bus.$on('project.task.edit.remunerationTypeModel', function(result) {
          if (result) {
            that.showFlag = true
          } else {
            that.showFlag = false
          }
        })
      },
      addRule() {
        const ruleType = this.ruleType
        if (!this.$utils.isExist(ruleType)) {
          this.$message({
            type: 'warning',
            message: '请选择规则类型'
          })
          return
        }
        var data = {
          ruleType: ruleType,
          needFlag: '0'
        }
        const dataList = this.dataList
        dataList.push(data)
        this.dataList = dataList
      },
      changeNeedFlag(row) {
        const flag = row.needFlag
        if (flag === '1') {
          row.needFlag = '0'
        } else {
          row.needFlag = '1'
        }
      },
      // 交换数组元素
      swapItems(arr, index1, index2) {
        arr[index1] = arr.splice(index2, 1, arr[index1])[0]
        return arr
      },
      // 上移
      upRecord(index) {
        const dataList = this.dataList
        if (index === 0) {
          return
        }
        if (index === 1) {
          return
        }
        this.swapItems(dataList, index, index - 1)
      },
      // 下移
      downRecord($index) {
        const dataList = this.dataList
        if ($index === dataList.length - 1) {
          return
        }
        this.swapItems(dataList, $index, $index + 1)
      },
      // 删除记录
      deleteRecord(index) {
        const dataList = this.dataList
        dataList.splice(index, 1)
        this.dataList = dataList
      },
      // 显示规则类型
      showRuleType() {
        this.visible = true
      }
    },
    watch: {
      dict: function(val, oldval) {
        const ruleSettingType = val.ruleSettingType

        const textDict = {}
        const fileDict = {}
        for (const key in ruleSettingType) {
          if (key.startsWith('1')) {
            textDict[key] = ruleSettingType[key]
          }
          if (key.startsWith('2')) {
            fileDict[key] = ruleSettingType[key]
          }
        }
        this.myDict = val
        this.myDict.textDict = textDict
        this.myDict.fileDict = fileDict
      }
    }
  }
</script>
